package com.tengju.bff.interfaces.manage.common;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

import com.tengju.bff.interfaces.manage.common.facade.FileFacade;
import com.tengju.bff.interfaces.manage.common.vo.UploadFileVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.SystemException;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.user.application.common.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "文件操作接口")
@RequestMapping(value = "manage/file")
@Slf4j
@ManageEntrance
public class FileController {

    @Autowired
    private FileService fileService;
    @Resource
    private FileFacade fileFacade;

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> uploadFile(@RequestParam("file") MultipartFile multipartFile) {
        try {
            return ApiResponse.newSuccess(fileService.uploadImg(multipartFile));
        } catch (SystemException e) {
            log.error("文件上传失败,", e);
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            log.error("文件上传失败,", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "文件上传，限制条件", notes = "文件上传，限制条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name="file", value="上传的文件"),
            @ApiImplicitParam(name="limitWidth", value="限制图片文件宽度，非必填，传入时如果是图片，超过高度会报错"),
            @ApiImplicitParam(name="limitHeight", value="限制图片文件高度，非必填，传入时如果是图片，超过高度会报错"),
            @ApiImplicitParam(name="limitSize", value="限制文件大小，非必填，传入时超过大小会报错"),
    })
    @RequestMapping(value = "limitUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<UploadFileVO> limitUpload(
            @RequestParam("file") MultipartFile multipartFile,
            @RequestParam("limitWidth") Integer limitWidth,
            @RequestParam("limitHeight") Integer limitHeight,
            @RequestParam("limitSize") Long limitSize) {
        try {
            return ApiResponse.newSuccess(fileFacade.upload(multipartFile, limitWidth, limitHeight, limitSize));
        } catch (SystemException e) {
            log.error("图片上传失败,", e);
            return ApiResponse.newFailure(e.getErrorCode(), e.getErrorMessage());
        } catch (Exception e) {
            log.error("图片上传失败,", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }
}
